var billManageService = {
	search : function(param){
		if(!param) param = {};
		if(param.pageIndex) {
			this.extend(param, this.searchData);
		} else{
			this.statusList && this.statusList.forEach(function(item){
				if(item.name === param.status) param.status = item.code;
				// if(item.name === '全部') delete param.status;
			});
			this.extend(param, {pageSize : this.pageSize});
		}
		param.customerId = this.cookie();
		this.selectItem = {};
		this.setState({selectItem : this.selectItem, selectLength : 0});
		if(param.billNo) param.billNo = $.trim(param.billNo);
		if(param.orderNo) param.orderNo = $.trim(param.orderNo);
		Api.get("billManage.getBillList", {'urlParams' : param }, this.handleSearchResult);
	},
	extend : function(dist, source){
		for(var i in source) if(source[i]) dist[i] = source[i];
	},
	handleSearchResult : function(res){
		if(res.errorCode == 0)	this.setState({list : res.data, total : res.pagination.totalResult});
	},
	enableButton : function() {
	},
	disableButton : function(data) {
	},
	cookie : function(){
		if(typeof document == 'undefined') return;
		var cook = document.cookie.split(";");
		var userId;
		cook.map(function(c){
			if(c.indexOf('userId') != -1){
				userId = c.split("=")[1];
			}
		});
		if(userId) return userId;
	},
	isPrimary : function(){
		if(typeof document == 'undefined') return;
		var cook = document.cookie.split(";");
		var primary;
		cook.map(function(c){
			if(c.indexOf('primary') != -1){
				primary = c.split("=")[1];
			}
		});
		if(primary === 'true') return true;
		return false;
	},
	submit : function(data) {
	  this.refs.form.setFormPristine();
	  this.searchData = data;
	  this.search(data);
	},
	resetForm : function() {
	   this.refs.form.setFormPristine();
	   this.refs.form.reset();
	   delete this.searchData;
	   this.refs.starttime.setValue(new Date().getLastMonth().start);
	   this.refs.endtime.setValue(new Date().getLastMonth().end);

	   this.search({
		    startEffectTime:new Date().getLastMonth().start,
			endEffectTime:new Date().getLastMonth().end});
	},
	onPageChange : function(pageIndex,pageSize){
		var param = {pageSize: pageSize, pageIndex:pageIndex,selectItem:{},selectLength:0};
		this.pageSize = pageSize;
		this.search(param);
	},
	selectItemFn : function(item,e){
		var checked = e.target.checked, index, self = this, list = this.state.list, length = this.state.selectLength || 0, data;
		
		if(!this.selectItem) this.selectItem = {};
		if(item === 'all'){
			if(checked){
				list.forEach(function(item){self.selectItem[item.billNo] = item});
				length = list.length;
			} 
			else{
				for(var i in this.selectItem) delete this.selectItem[i];
				length = 0;
			}
		}else{
			data  = item.billNo;
			if(checked){
				this.selectItem[data] = item;
				length += 1;
			}	
			else{
				delete this.selectItem[data];
				length -= 1;
			} 
		}
		this.setState({selectItem : this.selectItem, selectLength : length});
	},
	canHandle : function(type){
		var data = this.selectItem,
            Item = 0,
            otherItem = 0;

        if(!this.state.selectLength) return false;

        for(var i in data){
        	if(data[i].status != type) otherItem++;
        	else Item++;
        }

        return !(otherItem === 0 && Item > 0);
	},
	getStatusList : function(){
		var self = this;
		Api.get("billManage.billingStatus", function(res){
			if(!res.data) return;
			res.data.unshift({name: '全部'});
			self.statusList = res.data;
			selectFactory({data: res,id: "status",offset: -300,attrTextModel:function(name){
				self.refs.status.setValue(name);
			}});
		});
	},
	getSelectedItem : function(){
		var items = [];
		for(var i in this.selectItem){
			items.push(i);
		}
		return items;
	},
	showDialog : function(type){
		$('iframe').remove();
		if(this.getSelectedItem().length === 0){
			this.handlerMsg({msg : '请先选择数据', open : true});
			return;
		} 
		var dialog = {icon : 1, open : 1};
		if(type === 'delete'){
			dialog.message = '确认要删除所选账单吗？';
			this.dialogFn = 'deleteBill';
		}else if(type === 'export'){
			dialog.message = '确定要导出所选账单吗？'
			this.dialogFn = 'exoprtBill';
		}
		this.setState({dialog:dialog});
	},
	deleteBill : function(){
		var items = this.getSelectedItem(), self = this;
		if(items.length > 0){
			Api.post("billManage.deleteBill",{'urlParams' : items }, function(res){
				self.handlerMsg(res);
			});
		}
	},
	closeMsg : function(){
		this.setState({msg : {open : false}});
	},
	exoprtBill : function(){
		var items = this.getSelectedItem(), self = this;
		if(items.length > 0){
			Api.post("billManage.export",{'urlParams' : items }, function(res){
				console.log(res, ' res');
				self.handlerMsg(res);
			});
		}
	},
	handlerMsg : function(res){
		var self = this;
		if(res.errorCode == 0){

			res.data.forEach(function(bill){
				self.downloadFile(bill.url);	
			});
		}else{
			this.setState({msg:{open:true, message: res.msg,error : true}});
		}
	},
	confirm : function(){
		if(this.dialogFn) this[this.dialogFn]();
		this.setState({dialog : {open: false}});
	},
	downloadFile : function(url) {
		
        try{ 
            var elemIF = document.createElement("iframe");   
            elemIF.src = url;   
            elemIF.style.display = "none";   
            document.body.appendChild(elemIF);   
        }catch(e){ 
 
        } 
    }
}
module.exports = billManageService;